package com.heima.article.controller;

import com.alibaba.fastjson.JSON;
import com.heima.article.entity.ApArticle;
import com.heima.article.es.ApArticleSearch;
import com.heima.article.es.ApArticleSearchRepository;
import com.heima.article.service.IApArticleService;
import com.heima.common.dto.ResponseResult;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 创建es索引
     *
     * @return
     */
    @GetMapping("/createIndex")
    public ResponseResult createIndex() {
        elasticsearchRestTemplate.createIndex(ApArticleSearch.class);
        return ResponseResult.okResult("索引创建完成");
    }

    /**
     * 添加映射
     *
     * @return
     */
    @GetMapping("/putMapping")
    public ResponseResult putMapping() {
        boolean putMapping = elasticsearchRestTemplate.putMapping(ApArticleSearch.class);
        return ResponseResult.okResult(putMapping);
    }

    /**
     * 保存文章别表
     */
    @Autowired
    private IApArticleService articleService;

    @GetMapping
    public ResponseResult addDoc() {
        List<ApArticle> articles = articleService.list();
        for (ApArticle article : articles) {
            ApArticleSearch apArticleSearch = new ApArticleSearch();
            BeanUtils.copyProperties(article, apArticleSearch);
            //保存到es
            IndexQuery insexQuery = new IndexQuery();
            insexQuery.setId(apArticleSearch.getId().toString());
            insexQuery.setObject(apArticleSearch);
            elasticsearchRestTemplate.index(insexQuery);
        }
        return ResponseResult.okResult();
    }

    @Autowired
    private ApArticleSearchRepository repository;

    @GetMapping("/del")
    public ResponseResult deleteAll(){
        repository.deleteAll();
        return ResponseResult.okResult();
    }
    @GetMapping("/add")
    public ResponseResult addAll(){
        List<ApArticle> list = articleService.list();
        ArrayList<ApArticleSearch> searches = new ArrayList<>();
        for (ApArticle article : list) {
            ApArticleSearch search=new ApArticleSearch();
            BeanUtils.copyProperties(article,search);
            searches.add(search);
        }
        repository.saveAll(searches);
        return ResponseResult.okResult();
    }
    @GetMapping("/abc")
    public ResponseResult abc(String abc){
        QueryBuilder query= QueryBuilders.rangeQuery(abc);
        Iterable<ApArticleSearch> search = repository.search(query);
        for (ApArticleSearch apArticleSearch : search) {
            String string = JSON.toJSONString(apArticleSearch);
            System.out.println(string);
        }
        return ResponseResult.okResult();
    }
}
